package com.company.阻塞队列;

import sun.awt.windows.ThemeReader;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @ProjectName: 复习java
 * @Package: com.company.阻塞队列
 * @ClassName: demo4
 * @Author: wym
 * @Date: 2020/9/29 17:14
 * @Version: 1.0
 */
class ShareRes{
    //a 1 b2 c3
    private Integer number=1;
    private Lock lock=new ReentrantLock();
    private Condition c1=lock.newCondition();
    private Condition c2=lock.newCondition();
    private Condition c3=lock.newCondition();
    //判断
    public void  print5(){
        lock.lock();
        try{
            while (number!=1){
                //停止
                c1.await();
            }
            for (int i = 0; i <5 ; i++) {
                System.out.println(Thread.currentThread().getName()+"\t"+i);
            }
            //通知B
            number=2;
            c2.signalAll();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
    public void  print10(){
        lock.lock();
        try{
            while (number!=2){
                //停止
                c2.await();
            }
            for (int i = 0; i <10 ; i++) {
                System.out.println(Thread.currentThread().getName()+"\t"+i);
            }
            //通知B
            number=3;
            c3.signalAll();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
    public void  print15(){
        lock.lock();
        try{
            while (number!=3){
                //停止
                c1.await();
            }
            for (int i = 0; i <15 ; i++) {
                System.out.println(Thread.currentThread().getName()+"\t"+i);
            }
            //通知B
            number=1;
            c1.signalAll();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
    //干活

    //通知
}
public class demo4 {
    public static void main(String[] args) {
        ShareRes shareRes = new ShareRes();

        new Thread(()->{
            try {
                for (int i = 0; i < 5; i++) {
                    shareRes.print5();
                }
            }catch (Exception e){

            }
        },"a").start();
        new Thread(()->{
            try {
                for (int i = 0; i < 10; i++) {
                    shareRes.print10();
                }
            }catch (Exception e){

            }
        },"b").start();
        new Thread(()->{
            try {
                for (int i = 0; i < 15; i++) {
                    shareRes.print15();;
                }
            }catch (Exception e){

            }
        },"c").start();
    }
}
